from django.core.files.storage import FileSystemStorage
from django.db import models

# Create your models here.


class Script(models.Model):
    script_id = models.AutoField(
        auto_created=True, primary_key=True, serialize=False, verbose_name='ID', db_column='id')
    name = models.CharField(max_length=255, blank=True,
                            null=True, verbose_name='name')
    script_type = models.CharField(
        max_length=255, blank=True, null=True, db_column='type', verbose_name='type')
    desc = models.CharField(max_length=500, blank=True,
                            null=True, verbose_name='desc')
    time_out = models.PositiveIntegerField(
        blank=True, null=True, verbose_name='time_out')
    script_path = models.CharField(
        max_length=500, blank=True, null=True, verbose_name='script_path')
    user_id = models.CharField(
        max_length=255, blank=True, null=True, verbose_name='user_id')

    tenant_id = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField(blank=True, null=True, auto_now_add=True)
    updated_at = models.DateTimeField(blank=True, null=True,auto_now=True)
    deleted_at = models.DateTimeField(blank=True, null=True)
    #返回属性字典
    @property
    def meta_dict(self):
        
        return {
        'id': str(self.script_id or ''),
        'name': str(self.name or ''),
        'type': str(self.script_type or ''),
        'desc':str(self.desc or ''),
        'script_path':str(self.script_path or ''),
        'time_out':str(self.time_out or ''),
        'tenant-id':str(self.tenant_id or ''),
        'created_at':str(self.created_at or ''),
        'updated_at':str(self.updated_at or ''),
        'delete_at':str(self.deleted_at or '')
        }  
    @classmethod
    def create(cls, script_info:dict):
        script = cls(
            name=script_info.get('name'),
            script_type=script_info.get('script_type'),
            tenant_id=script_info.get('tenant_id'),
            desc=script_info.get('desc'),
            time_out=script_info.get('time_out'),
            user_id=script_info.get('user_id')
        )
        return script

    class Meta:
        managed = False
        db_table = 'script'


class ScriptRunningHosts(models.Model):
    script_running_history_id = models.PositiveIntegerField(
        blank=True, null=True)
    status = models.CharField(max_length=255, blank=True, null=True)
    ip = models.CharField(max_length=255, blank=True, null=True)
    host_name = models.CharField(max_length=255, blank=True, null=True)
    run_user = models.CharField(max_length=255, blank=True, null=True)
    result = models.TextField(blank=True, null=True)
    exit_code = models.IntegerField(blank=True, null=True)
    created_at = models.DateTimeField(blank=True, null=True)
    deleted_at = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'script_running_hosts'


class ScriptRunningHistory(models.Model):
    history_id = models.AutoField(
        auto_created=True, primary_key=True, serialize=False, verbose_name='ID', db_column='id')
    script_id = models.PositiveIntegerField(blank=True, null=True)
    user_id = models.CharField(max_length=255, blank=True, null=True)
    user_name = models.CharField(max_length=255, blank=True, null=True)
    script_params = models.CharField(max_length=500, blank=True, null=True)
    started_at = models.DateTimeField(blank=True, null=True,auto_now_add=True)
    end_at = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'script_running_history'
